package com.can.callableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //Thread怎么启动Callable
//        new Thread(new Runnable()).start();
//        new Thread(new FutureTask<V>()).start();
//        new Thread(new FutureTask<V>(Callable)).start();
        MyThread myThread = new MyThread();
        //适配类
        FutureTask futureTask = new FutureTask(myThread);
        new Thread(futureTask,"A").start(); //Thread调用Callable
        new Thread(futureTask,"B").start(); //结果会被缓存，效率高
        String o = (String) futureTask.get();//获取Callable的返回值   //这个get方法可能会产生阻塞! //或者使用异步通信来处理
        System.out.println(o);

    }
}

//泛型就是call方法返回值
class MyThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("call()");
        return "10458577717";
    }
}
